package com.woniuxy.yuekeuser.controller;


import com.woniuxy.yuekeuser.common.Result;
import com.woniuxy.yuekeuser.info.PageInfo;
import com.woniuxy.yuekeuser.model.CampsiteActivity;
import com.woniuxy.yuekeuser.model.CampsiteFood;
import com.woniuxy.yuekeuser.service.ICampsiteActivityService;
import com.woniuxy.yuekeuser.service.ICampsiteFoodService;
import com.woniuxy.yuekeuser.service.ICampsiteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Freedom
 * @since 2020-11-03
 */
@RestController
@Slf4j
public class CampsiteFoodController {

    @Autowired
    private ICampsiteService iCampsiteService;

    @Autowired
    private ICampsiteFoodService iCampsiteFoodService;



    /**
     * 根据营地名查询美食(分页)
     * @return
     */
    @GetMapping("getFoodByCampsiteName")
    public Result getFoodByCampsiteName(@RequestParam(defaultValue = "1") int pageIndex, @RequestParam(defaultValue = "4") int pageSize, String campsiteName){
        log.info("名字{}",campsiteName);
        int index = (pageIndex-1)*pageSize;
        //分页查询营地美食
        List<CampsiteFood> foods = iCampsiteFoodService.findFoods(campsiteName, index, pageSize);
        for(CampsiteFood food : foods){
            food.setImage(iCampsiteFoodService.findFoodImageByFoodId(food.getCampsiteFoodId()));
        }
        //获取总条数
        int count = iCampsiteFoodService.findFoodCount(campsiteName);
        //获取总页码
        int countPage = -1;
        if(count<pageSize){
            countPage = 1 ;
        }else{
            countPage = (count%pageSize ==0 ? count/pageSize : (count/pageSize)+1);
        }
        PageInfo pageInfo = new PageInfo();
        pageInfo.setList(foods);
        pageInfo.setCount(count);
        pageInfo.setCountPage(countPage);
        pageInfo.setPageIndex(pageIndex);
        return  Result.success(pageInfo);
    }

}

